/**
 * Title:        Utility Class Library<p>
 * Description:  Some useful utility classes.<p>
 *
 */
package com.timber.util;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JTable;

import pv.jfcx.JPVTable;

/**
 * Class used to format and render a table cell
 * 
 * @author groytvar
 *
 */
public class NumberRenderer extends SLNumberRenderer
{
  /*protected static NumberFormat	nf = NumberFormat.getInstance();

  protected int		integerPlaces;
  protected int		decimalPlaces;
  protected int		alignment;
  */

  protected boolean	showNegInRed;

/**
 * 
 * @param integerPlaces
 * @param decimalPlaces
 * @param alignment
 */
  public NumberRenderer( int integerPlaces, int decimalPlaces, int alignment )
  {
    this( integerPlaces, decimalPlaces, alignment, false );
  }

  /**
   * 
   * @param integerPlaces
   * @param decimalPlaces
   * @param alignment
   * @param showNegInRed
   */
  public NumberRenderer( int integerPlaces, int decimalPlaces, int alignment, boolean showNegInRed )
  {
    super( JPVTable.DOUBLE, integerPlaces, decimalPlaces, alignment );
    this.showNegInRed  = showNegInRed;
  }

  /*
  protected void setValue( Object value )
  {
    if( value != null && value instanceof Number )
    {
      double dval = ( ( Number )value ).doubleValue();

      if( dval != 0.0 )
	setText( nf.format( dval ) );
      else
	setText( "" );
      
      if( dval < 0.0 )
      {
	setForeground( Color.red );
      }
      else
      {
	setForeground( Color.black );
      }
    }

    else
      {

      super.setValue( value );
      }
    setHorizontalAlignment( alignment );
  }
  */

  public Component getTableCellRendererComponent( JTable  t,
						  Object  o,
						  boolean sel,
						  boolean foc,
						  int     row,
						  int     col )
  {
    if( showNegInRed == true )
    {
      if( o != null && o instanceof Number && ( (Number)o ).doubleValue() < 0.0 )
	setForeground( Color.red );
      else
	setForeground( Color.black );
    }

    Component ret = super.getTableCellRendererComponent( t, o, sel, foc, row, col );
    return ret;

  }

}
